package com.lagou.monitor;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.time.LocalTime;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


/**
 * @Authther: mrwu
 * @date:2021/4/11-04
 * @Description:com.lagou.monitor
 */
@Slf4j
@Activate(group = {CommonConstants.CONSUMER})
public class TP90Monitor implements Filter{

    JedisPool jedisPool = new JedisPool("127.0.0.1");
    private final ArrayList<String> methods = new ArrayList<>();

    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String methodName = invocation.getMethodName();//获取当前拦截的方法名称
        //获取当前时间
        Long start = System.currentTimeMillis();
        try {
            return  invoker.invoke(invocation);
        } finally {
            //获取响应后的时间
            Long end = System.currentTimeMillis();
            Long response = end-start;
            synchronized (this){
                if (!methods.contains(methodName)){
                    methods.add(methodName);
                }
            }
            Jedis jedis =jedisPool.getResource();
            String setex = jedis.setex("tp:" + methodName + ":" + end, 60, response.toString());
            jedis.close();
        }
    }

    public TP90Monitor() {
        //每5秒定时执行一次
        Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                Jedis  jedis= jedisPool.getResource();
                //获取当前时间
                Long current = System.currentTimeMillis();
                System.out.println("执行方法"+methods.size());
                for (int i = 0; i < methods.size(); i++) {
                    String methodname = methods.get(i);
                    Set<String> keys = jedis.keys("tp:" + methodname+"*");
                    int size = keys.size();//过去一分钟的请求数量
                    ArrayList<String> array = new ArrayList<String>();
                    //将所有key的值存入数组
                    keys.forEach((key)->{
                        String s =jedis.get(key);
                        array.add(s);
                    });
                    //排序
//                    System.out.println(array+"aa");
                    Object[] objects = array.toArray();
                    Arrays.sort(objects);
                    double ceil = Math.ceil(size * 0.90);
                    String time = LocalTime.now().toString();
                    System.out.println(time+":"+methodname+"当前的tp90时间为"+objects[(int) ceil]+"过去一分钟请求数为"+size);
//                    log.info(methodname+"当前的tp90时间为"+objects[(int) ceil]+"过去一分钟请求数为"+size);
                    double ceil99 = Math.ceil(size * 0.99);

                    System.out.println(time+":"+methodname+"当前的tp99时间为"+objects[(int) ceil99]+"过去一分钟请求数为"+size);
//                    log.info(methodname+"当前的tp99时间为"+objects[(int) ceil99]+"过去一分钟请求数为"+size);

                }
                jedis.close();
            }
        }, 1, 5, TimeUnit.SECONDS);
    }

}
